package com.wang.project.email;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 邮箱验证码
 */
@Component
@Slf4j
public class EmailSendCodeHandel {

    @Autowired(required = false)
    private JavaMailSender emailSender;
    final String myEmailHost = "wang546385234@qq.com";

    public void sendCode(String code, String email) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("openApi邮箱登录"); // 发送邮件的标题
        message.setText("登录操作，验证码：" + code + "，切勿将验证码泄露给他人，本条验证码有效期2分钟。"); // 发送邮件的内容
        message.setTo(email); // 指定要接收邮件的用户邮箱账号
        message.setFrom(myEmailHost); // 发送邮件的邮箱账号，注意一定要和配置文件中的一致！
        log.info("getEmailVerificationCode email: {} ,code : {}", email, code);
        emailSender.send(message); // 调用send方法发送邮件即可
    }

}
